package com.softserveinc.moviedbapp.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

import com.softserveinc.moviedbapp.manager.DataManager;

/* receives messages when there's changes in network state */
public class ConnectivityReceiver extends BroadcastReceiver {

	private DataManager mDataManager;
	private String TAG = ConnectivityReceiver.class.getSimpleName();


	public ConnectivityReceiver(final DataManager dm) {
		mDataManager = dm;
	}

	@Override
	public void onReceive(final Context context, final Intent intent) {
		Log.i(TAG, "onReceive()");
		NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

		if (info != null) {
			String state = info.getState().toString();
			if (state.equals("CONNECTED")) {
				mDataManager.setIsConnected(true);
			} else if (state.equals("DISCONNECTED")) {
				mDataManager.setIsConnected(false);
			}
			Log.i(TAG, state);
			Toast.makeText(context, state, Toast.LENGTH_SHORT).show();
		}
	}
}
